package com.example.bbs.util;

import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.RecyclerView;

public class DisplayUtil {

    static final String LOG_TAG = DisplayUtil.class.getSimpleName();
    public static final int ZERO_HEIGHT = 1; // 如果一些组件设置为 0 dp，会出现闪烁的问题，因此 1 是最小高度。
    public static Point screenSize;
    public static Point screenSizeInDp;
    public static float screenAspectRatio;
    public static float density;
    public static final int REFRESHING_PAGE_WAITING_TIME = 150; // milliseconds
    public static final int VIEW_PAGER_SHIFTING_TIME = 300; // milliseconds

    public static void init(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;
        int height = dm.heightPixels;
        density = dm.density;
        Log.d(LOG_TAG, "window width: " + width + ", window height: " + height + ", density: " + density);
        // int densityDpi = dm.densityDpi; // density = 1 对应 densityDpi = 160
        screenSize = new Point(width, height);
        screenSizeInDp =  new Point((int) (width / density), (int) (height / density));
        screenAspectRatio = (float) width / height;
    }

    private DisplayUtil() throws InstantiationException {
        throw new InstantiationException("This class is not for instantiation");
    }

    public static int getSoftKeyBoardHeight(Activity activity) {
        int screenHeight = activity.getWindow().getDecorView().getHeight();
        Rect visibleRect = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(visibleRect);
        int resourceId = activity.getResources().getIdentifier(
                "navigation_bar_height", "dimen", "android");
        int navigationBarHeight = activity.getResources().getDimensionPixelSize(resourceId);

        return screenHeight - navigationBarHeight - visibleRect.bottom;
    }

    public static int dp2px(float dp) {
        return (int) (dp * density + 0.5f);
    }

    /** 如果直接调用 setLayoutParams, 会导致 ConstraintLayout 布局混乱 */
    public static void setViewSize(View v, int width, int height) {
        ViewGroup.LayoutParams params = v.getLayoutParams();
        if (params == null)
            params = new ViewGroup.LayoutParams(width, height);
        else {
            params.width = width;
            params.height = height;
        }
        v.setLayoutParams(params);
    }

    public static void setMargin(View v, int left, int top, int right, int bottom) {
        try {
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) v.getLayoutParams();
            if (params == null)
                params = new LinearLayout.LayoutParams(0, 0);
            params.setMargins(left, top, right, bottom);
            v.setLayoutParams(params);
        } catch (Exception ignored) { }
    }

    public static void cancelFocus(Context context, View v) {
        v.clearFocus();
        InputMethodManager inputMethodManager =
                (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputMethodManager.isActive()) {
            inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);// 隐藏输入法
        }
    }
}
